package com.boc.langxiang.presenter.me;


import com.boc.langxiang.contract.me.MyExerciseWantFragmentContract;
import com.boc.langxiang.entity.request.DelMytobecanyuRequest;
import com.boc.langxiang.entity.request.TobecanyuRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.MytobecanyuContnet;
import com.boc.langxiang.entity.response.TobecanyuContentBean;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MyExerciseWantFragmentPresenter implements MyExerciseWantFragmentContract.Presenter {

    private ApiService mService;
    private MyExerciseWantFragmentContract.View mView;

    public MyExerciseWantFragmentPresenter(MyExerciseWantFragmentContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        this.mView = view;
    }


    @Override
    public void mytobecanyu(String page) {
        mView.showLoading();
        TobecanyuRequest mytobecanyuRequest = new TobecanyuRequest();
        mytobecanyuRequest.page = page;
        Observable<BaseResponse<MytobecanyuContnet>> mytobecanyu = mService.mytobecanyu(mytobecanyuRequest.params());
        mytobecanyu.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<MytobecanyuContnet>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<MytobecanyuContnet> response) {
                        MytobecanyuContnet content = response.getContent();
                        if (content != null) {
                            List<TobecanyuContentBean> collect = content.getCollect();
                            int count = content.getCount();
                            mView.getMytobecanyu(collect, count);
                        } else {
                            mView.getMytobecanyu(null, 0);
                        }


                    }
                });
    }

    @Override
    public void loadMore(String page) {
        mView.showLoading();
        TobecanyuRequest mytobecanyuRequest = new TobecanyuRequest();
        mytobecanyuRequest.page = page;
        Observable<BaseResponse<MytobecanyuContnet>> mytobecanyu = mService.mytobecanyu(mytobecanyuRequest.params());
        mytobecanyu.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<MytobecanyuContnet>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<MytobecanyuContnet> response) {
                        MytobecanyuContnet content = response.getContent();
                        List<TobecanyuContentBean> collect = content.getCollect();
                        int count = content.getCount();
                        mView.getLoadMoreList(collect, count);
                    }
                });
    }

    @Override
    public void delMytobecanyu(String id, final int adapterPosition) {
        mView.showLoading();
        DelMytobecanyuRequest delMytobecanyuRequest = new DelMytobecanyuRequest();
        delMytobecanyuRequest.id = id;
        Observable<BaseResponse> delMytobecanyu = mService.delMytobecanyu(delMytobecanyuRequest.params());
        delMytobecanyu.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.deleteCanyuSuccess(adapterPosition);
                    }
                });

    }
}
